<?php
/**
 * 京东天气
 */
if (empty($regex_matches['city'])) {
    return;
}
$city = trim($regex_matches['city']);
if (mb_strlen($city, 'UTF-8') > 8) {
    return;
}
$url = 'https://way.jd.com/he/freeweather?city=%s&appkey=e61ea08206439db9cb30910865faad7c';
$obj = weather_http_request($url, $city);
if ($obj) {
    $data = $city . '今日天气：' . ($obj['now']['cond']['txt'] ?? '') . PHP_EOL .
        '风向：' . ($obj['now']['wind']['dir'] ?? '') . PHP_EOL .
        '风级：' . ($obj['now']['wind']['sc'] ?? '') . PHP_EOL .
        '空气质量：' . ($obj['aqi']['city']['qlty'] ?? '') . PHP_EOL .
        'pm25：' . ($obj['aqi']['city']['pm25'] ?? '') . PHP_EOL .
        '温度：' . ($obj['now']['tmp'] ?? '') . '摄氏度。' . PHP_EOL .
        '湿度：' . ($obj['now']['hum'] ?? '') . PHP_EOL .
        '能见度：' . ($obj['now']['vis'] ?? '') . PHP_EOL .
        '降水量：' . ($obj['now']['pcpn'] ?? '') . PHP_EOL .
        '感冒指数：' . ($obj['now']['fl'] ?? '') . PHP_EOL;
    echo $data;
}

/**
 * @param $url
 * @param $city
 * @return bool|array
 */
function weather_http_request($url, $city)
{
    $url = sprintf($url, $city);
    $ret = curl_http_request($url);
    $resp = json_decode($ret['response'], true);
    // 调试日志
    //file_put_contents(runtime_path() . '/userApi_weather.log', __METHOD__ . PP($resp, false));
    if (isset($resp['code']) && 10000 === (int)$resp['code']) {
        return $resp['result']['HeWeather5'][0];
    }

    return false;
}
